// // 测试接口根路径是否生效
// axios({
//     url: '/api/login',
//     method: 'post',
//     data: {
//       username: 'xiaowan',
//       password: 'xiaowan',
//     },
//   }).then((result) => {
//     console.log(result);
//   });


// axios({
//     url: '/api/register',
//     method: 'post',
//     data: {
//       username: 'qiaolao233',
//       password: 'qiaolao233',
//     },
//   }).then((result) => {
//     console.log(result);
//   });

{
    localStorage.setItem( 'token', '')
}

{
    const registerBox = document.querySelector('.register.box')
    const logInBox = document.querySelector('.login.box')
    const registerA = document.querySelector('.register-a')
    const logInA = document.querySelector('.login-a')
    const registerForm = document.querySelector('.register.box form')
    const logInForm = document.querySelector('.login.box form')

    {
        registerA.addEventListener( 'click', function () {
            registerBox.style.display = "none";
            logInBox.style.display = "block";
        })
        logInA.addEventListener( 'click', function () {
            logInBox.style.display = "none";
            registerBox.style.display = "block";
        })

        registerForm.addEventListener('submit', function (e) {
            e.preventDefault();
            const data = $(this).serialize();
            axios({ url: '/api/register', method: 'post', data }).then(res => {
                if (res.data.code === 0) {
                    toastr.success(res.data.message)
                    setTimeout(() => {
                        location.href = './index.html';
                    }, 1000)
                } else {
                    toastr.warning(res.data.message)
                }
            });
        })
        
        logInForm.addEventListener('submit', function (e) {
            e.preventDefault();
            const data = $(this).serialize();
            axios({ method: 'post', url: '/api/login', data }).then(res => {
                if (res.data.code === 0) {
                    toastr.success(res.data.message)
                    localStorage.setItem( 'token', res.data.token)
                    setTimeout(() => {
                        location.href = './index.html';
                    }, 1000)
                } else {
                    toastr.warning(res.data.message)
                }
            })
        })
    }
}